package logic

import (
	"context"
	"goods_srv/model/mysql"
	"goods_srv/model/redis"
	"gorm.io/gorm"
	"strconv"

	"goods_srv/goods"
	"goods_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateGoodsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateGoodsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateGoodsLogic {
	return &CreateGoodsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CreateGoodsLogic) CreateGoods(in *goods.CreateGoodsRequest) (*goods.CreateGoodsResponse, error) {
	// todo: add your logic here and delete this line
	g := mysql.Goods{
		Model:       gorm.Model{},
		GoodsTitle:  in.GoodsTitle,
		GoodsPrice:  float64(in.GoodsPrice),
		GoodsStock:  in.GoodsStock,
		GoodsStatus: in.GoodsStatus,
	}
	err := g.Create()
	if err != nil {
		logx.Error("创建失败", err)
		return nil, err
	}
	//同步虚拟库存
	redis.SynCGoodsStock(int(g.ID), int(g.GoodsStock))
	return &goods.CreateGoodsResponse{
		GoodsID: strconv.Itoa(int(g.ID)),
	}, nil
}
